package com.hanxu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 这是一种风格
 * http://localhost:8080/restfull?a=1&b=2
 * http://localhost:8080/restfull/1/2
 */
@Controller
public class RestfullController {

    /**
     * 访问 http://localhost:8080/restfull1?a=1&b=2
     * @param a
     * @param b
     * @param model
     * @return
     */
    @RequestMapping("/restfull1")
    public String test(int a , int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果1是"+res);
        return "test";
    }

    /**
     *访问 http://localhost:8080/restfull2/1/3
     *注意多的注释
     * @param a
     * @param b
     * @param model
     * @return
     */
    @RequestMapping(value = "/restfull2/{a}/{b}",method = RequestMethod.GET)
    public String test2(@PathVariable int a ,@PathVariable int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果2是"+res);
        return "test";
    }

    /**
     * 常常用方式
     * @param a
     * @param b
     * @param model
     * @return
     */
    //@GetMapping ( "/restfull2/{a}/{b}")
    //@DeleteMapping( "/restfull2/{a}/{b}")
    //@PostMapping( "/restfull2/{a}/{b}")

    @PutMapping( "/restfull2/{a}/{b}")
    public String test3(@PathVariable int a ,@PathVariable int b, Model model){
        int res = a + b;
        model.addAttribute("msg","结果2是"+res);
        //默认这里是转发
        return "test";
    }


    /**
     * 重定向的方式
     * @return
     */
    @GetMapping( "/redirect3")
    public String test4(){
        return "redirect:index.jsp";
    }

}
